package com.example.androidab;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import org.alicebot.ab.AB;
import org.alicebot.ab.AIMLProcessor;
import org.alicebot.ab.Bot;
import org.alicebot.ab.Category;
import org.alicebot.ab.Chat;
import org.alicebot.ab.Graphmaster;
import org.alicebot.ab.MagicBooleans;
import org.alicebot.ab.MagicNumbers;
import org.alicebot.ab.MagicStrings;
import org.alicebot.ab.Nodemapper;
import org.alicebot.ab.PCAIMLProcessorExtension;
import org.alicebot.ab.Verbs;
import org.alicebot.ab.utils.IOUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import dalvik.system.*;

public class MainActivity extends Activity {
	private Button btnSend;
	EditText edRequest;
	EditText edResponse;
	
    //Timer timer = new Timer();
    Bot bot;
    Chat chatSession;
    byte[] bytes ;
    private final static int CWJ_HEAP_SIZE = 128* 1024* 1024 ;   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 
		
		setMinHeapSize(CWJ_HEAP_SIZE);
		
		startAB();		
		btnSend = (Button) this.findViewById(R.id.btnSend);
		edRequest = (EditText) this.findViewById(R.id.editText1);
		edResponse = (EditText) this.findViewById(R.id.editText2);
		final Handler handle = new Handler(){
			@Override
			public void handleMessage(Message msg){
				Bundle b = msg.getData();
				edResponse.setText(b.getString("result"));
			}
		};
		btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				final String request = edRequest.getText().toString();
				new Thread(){
					public void run(){
						if (MagicBooleans.trace_mode) System.out.println("STATE="+request+":THAT="+chatSession.thatHistory.get(0).get(0)+":TOPIC="+chatSession.predicates.get("topic"));
						String response = chatSession.multisentenceRespond(request);
						while (response.contains("&lt;")) response = response.replace("&lt;","<");
						while (response.contains("&gt;")) response = response.replace("&gt;",">");
						Message msg = handle.obtainMessage();
						Bundle b = new Bundle();
						b.putString("result", response);
						msg.setData(b);
						msg.sendToTarget();
					}
				}.start();;
			}
		});
	
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
	public void startAB(){
		new Thread(){
			public void run(){
				setPriority (Thread.MAX_PRIORITY);
			   MagicStrings.setRootPath();
		
		       AIMLProcessor.extension =  new PCAIMLProcessorExtension();
		       String botName = "alice2";
		       MagicBooleans.jp_tokenize = false;
		       MagicBooleans.trace_mode = true;
		       String action="chat";
		       System.out.println(MagicStrings.program_name_version);
		
		       if (MagicBooleans.trace_mode) System.out.println("Working Directory = " + MagicStrings.root_path);
		       Graphmaster.enableShortCuts = true;
		       bot = new Bot(botName, "/sdcard/", action); //
		       if (MagicBooleans.make_verbs_sets_maps) 
		       	Verbs.makeVerbSetsMaps(bot);
		
		       chatSession = new Chat(bot, true);
		       bot.brain.nodeStats();
			}
		 }.start();
   }
    public static void setMinHeapSize(long size) {
        try {
                Class<?> cls = Class.forName("dalvik.system.VMRuntime");
                Method getRuntime = cls.getMethod("getRuntime");
                Object obj = getRuntime.invoke(null);// obj就是Runtime
                if (obj == null) {
                        System.err.println("obj is null");
                } else {
                        System.out.println(obj.getClass().getName());
                        Class<?> runtimeClass = obj.getClass();
                        Method setMinimumHeapSize = runtimeClass.getMethod(
                                        "setMinimumHeapSize", long.class);

                        setMinimumHeapSize.invoke(obj, size);
                }

        } catch (ClassNotFoundException e) {
                e.printStackTrace();
        } catch (NoSuchMethodException e) {
                e.printStackTrace();
        } catch (IllegalArgumentException e) {
                e.printStackTrace();
        } catch (IllegalAccessException e) {
                e.printStackTrace();
        } catch (InvocationTargetException e) {
                e.printStackTrace();
        }
    }
   
}
	
